package main

import (
	"flag"
	"fmt"
	"log"

	"gopkg.in/olivere/elastic.v6"

	"gitee.com/jnshao/go-start/projects/crawler_distributed/config"
	"gitee.com/jnshao/go-start/projects/crawler_distributed/persist"
	"gitee.com/jnshao/go-start/projects/crawler_distributed/rpcsupport"
)

// 命令行参数 go run itemsaver.go --port=1234
var port = flag.Int("port", 0, "the port for me to listen on")

func main() {
	flag.Parse()
	if *port == 0 {
		fmt.Println("must specify a port")
		return
	}
	//强制退出
	log.Fatal(serverRpc(fmt.Sprintf(":%d", *port), config.ElasticIndex))
}

// 启动rpc服务
func serverRpc(address, index string) error {
	client, err := elastic.NewClient(elastic.SetSniff(false))
	if err != nil {
		return err
	}
	err = rpcsupport.ServeRpc(address, &persist.ItemSaverService{ //指针接收者取地址
		Client: client,
		Index:  index,
	})
	return err
}
